<?php
/**
* @file Tmm.php
* @synopsis  测试Mongodb
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015-03-09 12:11:13
*/

defined('BASEPATH') OR exit('No direct script access allowed');

class Tmm extends CI_Model
{
	function __construct()
	{
        parent::__construct();
	}

	//添加一个关注
	public function insert_member_relation($data)
	{
		return $result = $this->db->insert("ml_member_relation",$data);
	}

	//获取我的关注
	public function get_followers($query,$limit = FALSE, $offset = FALSE)
	{
		return $this->db->select(array('followers','following','addtime'))->where($query)->order_by("addtime","desc")->get("ml_member_relation",$limit, $offset)->result_array();
	}

	//获取关注我的
	public function get_followings($query,$limit = FALSE, $offset = FALSE)
	{
		return $this->db->select(array('followers','following','addtime'))->where($query)->order_by("addtime","desc")->get("ml_member_relation",$limit, $offset)->result_array();
	}

	//删除关注
	public function remove_follow($followers, $following)
	{
        $query = array('followers'=>$followers, 'following'=>$following);
		if($query)  // 不能为空
        {
            $result = $this->db->where($query)->delete("ml_member_relation");     //只删除一条
            return $result;
        }
	}

    //获取关注数
    public function get_following_count($query)
    {
        return $result = $this->db->from("ml_member_relation")->where($query)->count_all_results();
    }

	//插入收藏
    public  function insert_favorite($sets)
    {
    	return $result = $this->db->insert("ml_favorite",$sets);
    }

    //删除收藏(只删除一条)
    public function remove_favorite($post_id,$user_id)
    {
        $query = array();
        $query['post_id'] = $post_id;
        $query['user_id'] = $user_id;

    	if($query)  // 不能为空
    	{
    		$result = $this->db->where($query)->delete("ml_favorite");     //只删除一条
    		return $result;	
    	}
    }

    //删除收藏(删除多条)
    public function remove_favorite_batch($query)
    {
        if($query)  // 不能为空
        {
            $result = $this->db->where($query)->delete("ml_favorite");     //只删除一条
            return $result; 
        }
    }

    //获取收藏数
    public function get_favorite_count($query)
    {
        $user_id = isset($query['user_id']) ? $query['user_id'] : 0;
        $post_id = isset($query['post_id']) ? $query['post_id'] : 0;
        if(!$user_id && !$post_id) return 0;
        $where = [];
        $where['b.is_show'] = 1;
        if($user_id) $where['a.user_id'] = $user_id;
        if($post_id) $where['a.post_id'] = $post_id;
        $this->db
            ->from("ml_favorite as a")
            ->join('ml_post as b', 'a.post_id=b.id', 'left')->where($where);
        if($query['favorite_addtime'])
        {
            $this->db->where("b.addtime<",$query['favorite_addtime']);
        }
        else
        {
            $this->db->where("b.addtime<",time());
        }
        if($query['no_qv'])
        {
            $this->db->where_in('b.post_type_extend', $query['post_type_extend']);
        }
        $result = $this->db->count_all_results();
        return $result;
        //return $result = $this->cimongo->count_all("favorite");
    }

    /**
     * 获取用户收藏数量
     * @param $user_id
     * @return int
     */
    public function get_user_favorite_count($user_id)
    {
        $user_id = isset($query['user_id']) ? $query['user_id'] : 0;
        $post_id = isset($query['post_id']) ? $query['post_id'] : 0;
        if(!$user_id && !$post_id) return 0;
        $where = [];
        $where['b.is_show'] = 1;
        if($user_id) $where['a.user_id'] = $user_id;
        if($post_id) $where['a.post_id'] = $post_id;
        $this->db
            ->from("ml_favorite as a")
            ->join('ml_post as b', 'a.post_id=b.id', 'left')->where($where);
        if($query['favorite_addtime'])
        {
            $this->db->where("a.addtime<",$query['favorite_addtime']);
        }
        else
        {
            $this->db->where("a.addtime<",time());
        }
        if($query['no_qv'])
        {
            $this->db->where_in('b.post_type_extend',array(1,2,3));
        }
        $result = $this->db->count_all_results();
        return $result;
        //return $result = $this->cimongo->count_all("favorite");
    }

    //获取收藏列表
    public function get_post_id($user_id,$favorite_addtime,$limit,$offset,$no_qv='')
    {
        $now_time = time();
        $this->db->select()->from("ml_favorite as a");
        $this->db->join('ml_post as b','a.post_id=b.id','left');
        $this->db->where('b.is_show',1);
        $this->db->where("a.user_id",(int)$user_id);
        if($no_qv)
        {
            $this->db->where_in('b.post_type_extend',array(1,2,3));
        }
        if($favorite_addtime)
        {
            $this->db->where("b.addtime<",$favorite_addtime);
        }
        else
        {
            $this->db->where("b.addtime<",$now_time);
        }

        $post_id_array = $this->db->order_by("a.id","desc")->limit($limit,$offset)->get()->result_array();
        return $post_id_array;
    }

    //获取收藏列表（文字及投票）
    public function get_qv_post_id($user_id,$favorite_addtime,$limit,$offset)
    {
        $now_time = time();
        $this->db->select()->from("ml_favorite as a");
        $this->db->join('ml_post as b','a.post_id=b.id','left');
        $this->db->where('b.is_show',1);
        $this->db->where("a.user_id",(int)$user_id);
        $this->db->where_in('b.post_type_extend',array(4,5));

        if($favorite_addtime)
        {
            $this->db->where("b.addtime<",$favorite_addtime);
        }
        else
        {
            $this->db->where("b.addtime<",$now_time);
        }

        $post_id_array = $this->db->order_by("a.id","desc")->limit($limit,$offset)->get()->result_array();
        return $post_id_array;
    }

    public function get_image_wh($original)
    {
        $img_path = ret_fullpath_url($original);
        //echo $img_path.'<br />';
        $img_info = $this->imagem->getimagesize($img_path);
        $width = $img_info[0];
        $height = $img_info[1];
        if($img_info)
        {
            $this->db->where("original",$original );
            $data = array('width'=>$width, 'height'=>$height);
            //w_log(json_encode($data));
            $this->db->update('ml_attachments', $data);
        }
    }
    public function get_image_url($start_id,$end_id)
    {
        return $this->db->select('original')->from('ml_attachments')->where("pid>=",$start_id)->where('pid<',$end_id)->get()->result_array();
    }

}
